#ifndef HTTPCONN_H
#define HTTPCONN_H

#include <atomic>
#include <sys/types.h>
#include <sys/uio.h>
#include <arpa/inet.h>  // readv/writev
#include <stdlib.h>     // sockaddr_in
#include <errno.h>      // atoi

#include "../log/log.h"
#include "../pool/sqlconnRAII.h"
#include "../buffer/buffer.h"
#include "httprequest.h"
#include "httpresponse.h"

class HttpConn {
public:
    HttpConn();
    ~HttpConn();

    void init(int socketFd, const sockaddr_in& addr);
    ssize_t read(int* saveErrno);
    ssize_t write(int* saveErrno);
    void close();
    
    int getFd() const;
    int getPort() const;
    const char* getIP() const;
    struct sockaddr_in getAddr() const;

    bool process();
    int toWriteBytes();
    bool isKeepAlive() const;
public:
    static bool isET;
    static const char* srcDir;
    static std::atomic<int> userCount;
private:
    int fd_;
    struct sockaddr_in addr_;

    bool isClose_;
    int iovCount_;          //  iovec结构体的数量
    struct iovec iov_[2];   //  用于分散读和集中写

    Buffer readBuff_;
    Buffer writeBuff_;

    HttpRequest request_;
    HttpResponse response_;
};

#endif